package git.soulbgm.thread;

import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import git.soulbgm.common.handler.ThreadBlockingHandler;
import git.soulbgm.service.AlarmInfoReport;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程工具类
 *
 * @author SoulBGM
 * @date 2022/01/17
 */
public class ThreadTool {

    /**
     * IO密集型线程池
     */
    public static ThreadPoolExecutor IO_INTENSIVE_THREAD_POOL;

    public static void init(AlarmInfoReport report) {
        int count = Runtime.getRuntime().availableProcessors();
        String namePrefix = "soul-monitor-io-thread-";
        ThreadFactory factory = ThreadFactoryBuilder.create().setNamePrefix(namePrefix).build();
        IO_INTENSIVE_THREAD_POOL = ExecutorBuilder.create()
                .setCorePoolSize(count).setMaxPoolSize(count * 3)
                .setWorkQueue(new LinkedBlockingQueue<>(count * 60))
                .setThreadFactory(factory)
                .setHandler(new ThreadBlockingHandler(report, namePrefix))
                .build();
    }

}
